import moment from 'moment'

moment.locale('zh_CN')

export default  {

getCurrentPosition:function ( cb ){
  new BMap.Geolocation().getCurrentPosition(function(r) {
    if (this.getStatus() == BMAP_STATUS_SUCCESS) {
      let location = {longitude: r.point.lng, latitude: r.point.lat , city : r.address.city.replace('市', '') };
          sessionStorage.setItem('city', location.city);
          sessionStorage.setItem('longitude', location.longitude);
          sessionStorage.setItem('latitude', location.latitude);
      cb&cb(location)

    }
  })
},
  isD11:function(){
    if((!sessionStorage.getItem('city')||sessionStorage.getItem('city')==='广州')&&Date.parse(new Date())<new Date('2017/11/11 07:10:00').getTime()){
      return false;
    }else{
      return false;
    }
  },
  getQueryString : function (name){
    var r = window.location.href.match(new RegExp("(\\?|&)" + name + "=([^&#]*)(&|#|$)", "i"));
    if(r) {
      return r[2];
    }
    return null;
  },
  formatTime : function (time , format ) {
    if (!time) return "";
    if (typeof time == "string") {
      time = parseInt(time, 10);
    }
    return moment(time).format(format || 'YYYY-MM-DD H:mm:ss')
  },
  formatDate:function (time , format ) {
    if (!time) return "";
    if (typeof time == "string") {
      time = parseInt(time, 10);
    }
    return moment(time).format(format || 'YYYY-MM-DD')
  },
   MillisecondToDate:function(msd) {
    var time = parseFloat(msd) /1000;
    if (null!= time &&""!= time) {
        if (time >60&& time <60*60) {
          let m=parseInt(time /60.0);
          let s=parseInt((parseFloat(time /60.0) -
            parseInt(time /60.0)) *60);
            time ='00:'+ (m>9?'':'0')+m +":"+ (s>9?'':'0')+s;
        }else if (time >=60*60&& time <60*60*24) {
          let h= parseInt(time /3600.0);
          let m=parseInt((parseFloat(time /3600.0) -
            parseInt(time /3600.0)) *60);
          let s=parseInt((parseFloat((parseFloat(time /3600.0) - parseInt(time /3600.0)) *60) - parseInt((parseFloat(time /3600.0) - parseInt(time /3600.0)) *60)) *60);
            time = (h>9?'':'0')+h +":"+ (m>9?'':'0')+m +":"+  (s>9?'':'0')+s;
        }else {
            time = "00:00:"+parseInt(time);
        }
    }else{
        time = "00:00:00";
    }
    return time;

}  ,
  firstDayOfMonth:function(){
    return moment().startOf('month');
  },
  endDayOfMonth:function(){
    return moment().endOf('month')
  },

  currentYear:function(){
    return moment().year()
  },

  currentMonth:function(){
    return moment().month() + 1
  },

  formatNumber:function(val , fixed){
    return parseFloat(val).toFixed(fixed)
  },
  formatDatas:function(num){
    let str = num+  '';
    let value = '';
    let ints = str;
    if(str.indexOf(".") > -1){
        let strArr = str.split(".");
        ints = strArr[0];
        let detail = strArr[1];

        if(parseInt(detail) == 0 ){
            return ints
        }else{
            return num
        }
    }else {
        return num
    }
  },

  toNumber:function(val){
    return Number(val)
  },

  isEmptyObject : function (e) {
    var t;
    for (t in e)
      return !1;
    return !0
  },
  encodeUrl:function(url){
    return encodeURI(url);
  },
  isWeixin:function(){
    let ua = navigator.userAgent.toLowerCase();
    return ua.indexOf('micromessenger') != -1;
  },

  isIOS : function(){
    var u = navigator.userAgent
    return u.indexOf('iPhone') > -1
  },

  getSysCode:function(value){ 
    var res;  
    let switchCityToSys = {
        '564701030634225664': '0002',
        '565541463497113600': '0004',
        '599411894062678016': '0003',
        '607495955062718464': "0003",
        '1027114332118241280': '0008',
        

    }
    for(let key in switchCityToSys){
        if(key===value){
            res = switchCityToSys[key]
        }
    }
    return res
  },
  install:function(Vue){
    Vue.filter('formatDate' ,this.formatDate )
    Vue.filter('formatTime' , this.formatTime )
    Vue.filter('formatNumber' , this.formatNumber )
    Vue.filter('formatDatas' , this.formatDatas )
    Vue.filter('toNumber' , this.toNumber )
    Vue.filter('MillisecondToDate' , this.MillisecondToDate )
    Vue.filter('encodeUrl' , this.encodeUrl )

    var isEmptyObject = this.isEmptyObject;
    Vue.mixin({
      methods: {
        isEmptyObject: isEmptyObject,
        encodeUrl:this.encodeUrl
      }
    })
  },




}
